package com.sunday.haowushop.ui.mine;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.recyclerView.HorizontalDividerItemDecoration;
import com.sunday.haowushop.R;
import com.sunday.haowushop.adapter.CommonAdapter;
import com.sunday.haowushop.adapter.ViewHolder;
import com.sunday.haowushop.entity.HelpList;
import com.sunday.haowushop.http.ApiClient;
import com.sunday.haowushop.ui.WebViewActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static android.Manifest.permission.CALL_PHONE;

/**
 * Created by 刘涛 on 2017/7/6.
 */

public class HelpCenterActivity extends BaseActivity {

    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;
    @Bind(R.id.btn_1)
    TextView btn1;
    @Bind(R.id.btn_2)
    TextView btn2;
    @Bind(R.id.btn_3)
    TextView btn3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_help_center);
        ButterKnife.bind(this);
        initRecyclerView();
        getData();
    }

    private CommonAdapter<HelpList> adapter;
    private List<HelpList> dataSet = new ArrayList<>();

    private void initRecyclerView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new CommonAdapter<HelpList>(mContext, R.layout.layout_help_list_item, dataSet) {
            @Override
            public void convert(ViewHolder holder, HelpList helpList) {
                bind(holder, helpList);
            }
        };
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(mContext).drawable(R.drawable.horizontal_space_divider).build());

    }

    private String url;
    private String title;

    private void bind(ViewHolder holder, final HelpList helpList) {
        ImageView img = holder.getView(R.id.help_img);
        if (!TextUtils.isEmpty(helpList.getImage())) {
            Glide.with(mContext)
                    .load(helpList.getImage())
                    .error(R.mipmap.ic_default)
                    .into(img);
        }
        TextView btnTotal = holder.getView(R.id.btn_total);
        TextView item1 = holder.getView(R.id.item1);
        TextView item2 = holder.getView(R.id.item2);
        TextView item3 = holder.getView(R.id.item3);
        TextView item4 = holder.getView(R.id.item4);
        ArrayList<TextView> textList = new ArrayList<>();
        textList.add(item1);
        textList.add(item2);
        textList.add(item3);
        textList.add(item4);
        btnTotal.setText(helpList.getName() + ">");
        if (!helpList.getList().isEmpty()) {
            for (int i = 0; i < 4; i++) {
                if (helpList.getList().size() > i) {
                    textList.get(i).setText(helpList.getList().get(i).getName());
                    textList.get(i).setTag(helpList.getList().get(i));
                } else {
                    textList.get(i).setText("");
                }
            }
        }
        View.OnClickListener onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.btn_total_layout:
                        Intent intent1=new Intent(mContext,HelpListActivity.class);
                        intent1.putExtra("title",helpList.getName());
                        intent1.putParcelableArrayListExtra("helpList", (ArrayList<HelpList>) helpList.getList());
                        startActivity(intent1);
                        break;
                    case R.id.item1:
                    case R.id.item2:
                    case R.id.item3:
                    case R.id.item4:
                        HelpList helplistItem = (HelpList) v.getTag();
                        if (helplistItem != null) {
                            url = String.format("http://admin.zj-yunti.com/DataController/helpInfo?id=%d", helplistItem.getId());
                            title = helplistItem.getName();
                            intent = new Intent(mContext, WebViewActivity.class);
                            intent.putExtra("title", title);
                            intent.putExtra("url", url);
                            startActivity(intent);
                        }
                        break;
                }
            }
        };
        holder.setOnClickListener(R.id.item1, onClickListener);
        holder.setOnClickListener(R.id.item2, onClickListener);
        holder.setOnClickListener(R.id.item3, onClickListener);
        holder.setOnClickListener(R.id.item4, onClickListener);
        holder.setOnClickListener(R.id.btn_total_layout,onClickListener);

    }


    private void getData() {
        showLoadingDialog(0);
        Call<ResultDO<List<HelpList>>> call = ApiClient.getApiAdapter().getHelpList();
        call.enqueue(new Callback<ResultDO<List<HelpList>>>() {
            @Override
            public void onResponse(Call<ResultDO<List<HelpList>>> call, Response<ResultDO<List<HelpList>>> response) {
                dissMissDialog();
                if (isFinish || response.body() == null) {
                    return;
                }
                if (response.body().getCode() == 0) {
                    dataSet.addAll(response.body().getResult());
                    adapter.notifyDataSetChanged();
                }else {
                    ToastUtils.showToast(mContext,response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO<List<HelpList>>> call, Throwable t) {
                dissMissDialog();
                ToastUtils.showToast(mContext, R.string.network_error);
            }
        });
    }


    @OnClick({R.id.btn_1, R.id.btn_2, R.id.btn_3})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_1:
                intent = new Intent(mContext, WebViewActivity.class);
                intent.putExtra("url",ApiClient.CUSTOMER_URL);
                intent.putExtra("title", "客服");
                startActivity(intent);
                break;
            case R.id.btn_2:
                intent = new Intent(mContext, WebViewActivity.class);
                intent.putExtra("url", ApiClient.CUSTOMER_URL);
                intent.putExtra("title", "客服");
                startActivity(intent);
                break;
            case R.id.btn_3:
                final String mobileNo="400-9999-423";
                AlertDialog dialog = new AlertDialog.Builder(mContext).setTitle("系统提示")//设置对话框标题
                        .setMessage("确认拨打电话")//设置显示的内容
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {//添加确定按钮
                            @Override
                            public void onClick(DialogInterface dialog, int which) {//确定按钮的响应事件
                                intent = new Intent();
                                intent.setAction(Intent.ACTION_CALL);
                                intent.setData(Uri.parse("tel:" + mobileNo));
                                if (ActivityCompat.checkSelfPermission(mContext, CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                                    ActivityCompat.requestPermissions((Activity) mContext, new String[]{
                                            CALL_PHONE
                                    }, 1);
                                    return;
                                }
                                startActivity(intent);
                            }
                        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {//添加返回按钮
                            @Override
                            public void onClick(DialogInterface dialog, int which) {//响应事件
                                dialog.dismiss();
                            }
                        }).show();//在按键响应事件中显示此对话框
                break;
        }
    }


}
